
from WtfEngine import *;
import WtfRts;
import Orders;
import Unit;
from scripts import *;

gController = 0;

class RtsGameState(CustomGameState):
	def __init__(self, camera):
		CustomGameState.__init__(self);
		self.camera = camera;
	
	def Enable(self):
		Renderer.GetInst().camera = self.camera;
		
		
# Manages and issues orders to the selected units
class Controller:
	def __init__(self):
		global gController;
		
		self.selectedUnits = [];
		self.listeners = [];
		
		self.commonOrders = [];
		self.orders = [Orders.StopOrder()];
		
		gController = self;
		
		Orders.Manager();
		
	def StartGame(self):
		# Camera setup
		camOpts = PannableCameraOptions();
		self.camera = PannableCamera(camOpts, Vector(0, 100, 100), Vector(0, -100, -100));
		
		# Push a fresh game state
		self.gameState = Kernel.GetInst().PushGameState(RtsGameState(self.camera));
		
		self.camera.BindToKeyboardAndMouse();
		
		self.unitSelection = WtfRts.UnitSelection();
		self.unitSelection.onChange = Function(lambda: self.OnUnitSelect());
		self.unitSelection.Init();
		
		
	def OnUnitSelect(self):
		DebugPrint("Selection Change");
		
		# Refresh unit selection
		self.selectedUnits = self.unitSelection.selection;
		
		# Enumerate intersection of orders
		if len(self.selectedUnits) > 0:
			self.orders = Orders.Manager.GetInst().allOrders.values();
			map(lambda unit: DebugPrint(unit.__class__.__name__), self.selectedUnits);
		else:
			self.orders = [];
		
		
		# Notify listeners
		map(lambda f: f(), self.listeners);
	
	def SetOrders(self, orders):
		self.orders = filter(lambda order: orders.count(order) > 0, self.orders);
		
	def InvokeOrder(self, order):
		return 0;
		
	def Listen(self, f):
		self.listeners.append(f);
	
	def GetInst():
		assert(gController != 0);
		return gController;
	GetInst = StaticM(GetInst);